package demo.hms.customer.service.servicelmpl;

import demo.hms.customer.dao.CustomerDao;
import demo.hms.customer.service.CustomerService;
import demo.hms.mybatis.entity.Customer;
import demo.hms.util.Result;
import demo.hms.util.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class CustomerServicelmpl implements CustomerService {
    @Resource
    private CustomerDao dao;
    SimpleDateFormat format =
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Result queryAll(Customer customer) {
        return ResultSubmit.success(dao.queryAll(customer));
    }

    @Override
    public Result insert(Customer customer) {
        customer.setCustomerId(Math.abs((int)System.currentTimeMillis()));
        customer.setDataUpdateTime(format.format(new Date()));
        customer.setDeleteIdentity("未删除");
        if (dao.insert(customer) > 0) {
            return ResultSubmit.success(customer);
        }
        // 手动回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultSubmit.fail();
    }
    @Override
    public Result update(Customer customer) {
        int i = dao.update(customer);
        customer.setDataUpdateTime(format.format(new Date()));
        customer.setDeleteIdentity("未删除");
        int j = dao.insert(customer);
        if(i > 0 && j > 0){
            return ResultSubmit.success(customer);
        }
        // 手动回滚
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        return ResultSubmit.fail();
    }

}
